package com.heavenick.core.mybatis.spring;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class SqlSessionFactoryCreaterBean extends SqlSessionFactoryBean {

	private final Log logger = LogFactory.getLog(getClass());

	public void setMapperLocations(String mapperLocations) {
		System.err.println("1234567890-==============================================");
		StringTokenizer st = new StringTokenizer(mapperLocations);
		Collection<Resource> collection = new ArrayList<Resource>();
		while (st.hasMoreTokens()) {
			String currToken = st.nextToken();
			PathMatchingResourcePatternResolver pathMatResolver = new PathMatchingResourcePatternResolver();
			Resource[] resources = new Resource[0];
			try {
				resources = pathMatResolver.getResources(currToken);
			} catch (IOException e) {
				logger.error("can't parse the mapper path : " + currToken);
			}
			for (Resource resource : resources) {
				collection.add(resource);
			}
		}
		super.setMapperLocations(collection.toArray(new Resource[collection
				.size()]));
	}
}
